package com.example.lib5.binaryTree.test;

/**
 * 创建日期：2022/6/11 0011 20:35
 *
 * @author tony.sun
 * 类说明：
 */
public class BinaryTreeE {
    public static void main(String[] args) {
        //创建二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建根节点
        Role root = new Role(1);
        Role role2 = new Role(2);
        Role role3 = new Role(3);
        Role role4 = new Role(4);
        Role role5 = new Role(5);
        //添加二叉树的数据
        root.setLeft(role2);
        root.setRight(role3);
        role3.setLeft(role5);
        role3.setRight(role4);
        binaryTree.add(root);
        //前中后序遍历二叉树
        System.out.println("前中后序遍历二叉树");
        binaryTree.preOrder();
        System.out.println("前中后序遍历二叉树");
        binaryTree.infixOrder();
        System.out.println("前中后序遍历二叉树");
        binaryTree.postOrder();
        //删除节点
        binaryTree.delete(5);
    }
}
class BinaryTree{

    private Role root;

    public void add(Role root) {
        this.root=root;
    }

    public void preOrder() {
        if (root!=null){
            root.preOrder();
        }else {
            System.out.println("节点为空");
        }
    }
    public void infixOrder() {
        if (root!=null){
            root.infixOrder();
        }else {
            System.out.println("节点为空");
        }
    }
    public void postOrder() {
        if (root!=null){
            root.postOrder();
        }else {
            System.out.println("节点为空");
        }
    }

    public void delete(int no) {
        if (root!=null) {
            if (root.getNo()==no) {
                root=null;
            }else {
                root.delete(5);
            }
        }else {
            System.out.println("节点为空");
        }
    }
}
class Role {

    private int no;
    Role left;
    Role right;

    public Role(int no) {
        this.no=no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Role getLeft() {
        return left;
    }

    public void setLeft(Role left) {
        this.left = left;
    }

    public Role getRight() {
        return right;
    }

    public void setRight(Role right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "Role{" +
                "no=" + no +
                '}';
    }

    public void preOrder() {
        System.out.println(this);
        if (this.left!=null) {
            this.left.preOrder();
        }
        if (this.right!=null) {
            this.right.preOrder();
        }

    }
    public void infixOrder() {

        if (this.left!=null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right!=null) {
            this.right.infixOrder();
        }

    }
    public void postOrder() {

        if (this.left!=null) {
            this.left.postOrder();
        }
        if (this.right!=null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    public void delete(int no) {
        if (this.left!=null&&this.left.no==no) {
            this.left=null;
            return;
        }
        if (this.right!=null&&this.right.no==no) {
            this.right=null;
            return;
        }
        if (this.left!=null){
            this.left.delete(no);
        }
        if (this.right!=null){
            this.right.delete(no);
        }
    }
}

